package com.glls.lambda.demo4;

import com.glls.lambda.pojo.Student;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @date 2022/10/3
 * @desc   java 内置的常用的函数式接口
 *
 * 四大基本函数式接口
 *
 * Consumer<T>    消费型接口    参数类型为T   返回值为void  void accept(T t)
 *
 * Supplier<T>    供给型接口    无参数   有返回值    T  get()
 *
 * Function<T,R>  函数型接口    参数类型为T   返回值为R  对类型为T的参数进行加工  返回一个 类型为R 的结果 R apply(T t)
 *
 * Predicate<T>   断言型接口    参数类型为T  返回值为boolean  对T类型参数进行判断 看是否满足条件 boolean test(T t)
 *
 *
 */
public class LambdaDemo4 {

    @Test
    public void test1(){
        //消费性接口
        playFoot(88.0,(money)->{
            System.out.println("77号技师服务不错，可以加钟，价格"+money+"钱");
        });

    }

    public void playFoot(Double money, Consumer<Double> consumer){
        consumer.accept(money);
    }

    @Test
    public void test(){
        study(()->{
            return "好好学习";
        });
    }

    public void study(Supplier<String> supplier){
        String s = supplier.get();
        System.out.println(s);
    }


    @Test
    public void test3(){
        study("你要偷偷的努力，然后惊艳所有人",(str)->{
            return str.substring(8);
        });
    }


    public void study(String str, Function<String,String> function){
        String apply = function.apply(str);

       System.out.println(apply);
    }

    List<Student> list = Arrays.asList(
            new Student(1,"zs",18,88.0),
            new Student(2,"ls",19,88.5),
            new Student(3,"ww",21,98.0),
            new Student(4,"zl",19,86.0),
            new Student(5,"tq",17,92.0)
    );

    @Test
    public void test4(){
        List<Student> result = filter(list, (student) -> {
            return student.getScore() > 90;
        });

        result.stream().forEach(System.out::println);
    }


    public List<Student> filter(List<Student> students, Predicate<Student> predicate){
        ArrayList<Student> result = new ArrayList<>();
        for(Student stu:students){
            if(predicate.test(stu)){
                result.add(stu);
            }
        }
        return result;
    }
}
